package top.jle.wx.proxy.util;

import lombok.RequiredArgsConstructor;
import org.apache.commons.text.RandomStringGenerator;

import static org.apache.commons.text.CharacterPredicates.DIGITS;
import static org.apache.commons.text.CharacterPredicates.LETTERS;

public class RandomStringUtil {

    @RequiredArgsConstructor
    private enum GeneratePolicy{
        LETTER_AND_DIGIT(new RandomStringGenerator.Builder().withinRange('0','z').filteredBy(LETTERS,DIGITS).build()),
        LETTER(new RandomStringGenerator.Builder().withinRange('a','z').filteredBy(LETTERS).build()),
        DIGIT(new RandomStringGenerator.Builder().withinRange('0','9').filteredBy(DIGITS).build()),
        ;

        private final RandomStringGenerator generator;
        public String generate(int length){
            return this.generator.generate(length);
        }
    }

    public static String randomLettersAndDigits(int length){
        return randomString(length,GeneratePolicy.LETTER_AND_DIGIT);
    }
    public static String randomLetters(int length){
        return randomString(length,GeneratePolicy.LETTER);
    }
    public static String randomDigits(int length){
        return randomString(length,GeneratePolicy.DIGIT);
    }

    private static String randomString(int length,GeneratePolicy generatePolicy){

        return generatePolicy.generate(length);
       
    }
}
